#!/usr/bin/python
# -- coding:utf8 --
import os
import sys

import pandas as pd

import utils
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import app.model_phl.v4.get_raw_data as getdata
import app.model_phl.v4.cal_userinfo_features as userinfo
import app.model_phl.v4.cal_app_features_v1 as app
import app.model_phl.v4.cal_sms_features_v1 as sms
import app.model_phl.v4.model_predict_v1 as md
import time
import warnings

warnings.filterwarnings('ignore')

# 从数据库获取的写法, 测试用
def get_free_features_v1(ApplyNO):
    try:
        # 1 用户基本信息特征集
        start = time.time()
        userinfo_features = userinfo.get_userinfo_freatures(ApplyNO)
        free_features = userinfo_features
        end = time.time()
        running_time = end - start
        print('1：用户基本信息特征模块计算耗费时间 : %.5f 秒' % running_time)

        # 2 获取APP和SMS数据集
        start = time.time()
        sms_df, app_df = getdata.get_raw_df(ApplyNO)
        end = time.time()
        running_time = end - start
        print('2：获取APP和SMS数据集计算耗费时间 : %.5f 秒' % running_time)

        # 3 APP特征集
        start = time.time()
        app_features = app.get_free_app_features(app_df)
        free_features.update(app_features)
        end = time.time()
        running_time = end - start
        print('3：APP特征模块计算耗费时间 : %.5f 秒' % running_time)

        # 3 SMS特征集
        start = time.time()
        sms_features = sms.get_free_sms_features(sms_df)
        free_features.update(sms_features)
        end = time.time()
        running_time = end - start
        print('4: SMS特征模块计算耗费时间 : %.5f 秒' % running_time)


        # 4 用户评分模块
        start = time.time()
        model_score_features = md.get_user_model_score_main(free_features)
        free_features.update(model_score_features)
        end = time.time()
        running_time = end - start
        print('5: 用户评分模块耗费时间 : %.5f 秒' % running_time)
        return model_score_features

    except Exception as e:
        # utils.get_logger().error(e)
        return {}

def calculate_free_features(free_features_params,AppList,SmsList):
    try:
        # 取出必备数据
        age = free_features_params['age']
        ApplyDate = free_features_params['ApplyDate']
        ApplyNO = free_features_params['ApplyNO']
        
        free_features = {}
        # 1 用户基本信息特征集
        userinfo_features = {}
        userinfo_features['age'] = age
        free_features.update(userinfo_features)

        # 2 获取APP和SMS数据集
        app_df = pd.DataFrame(AppList)
        app_df['ApplyDate'] = ApplyDate
        app_df['ApplyNO'] = ApplyNO
        sms_df = pd.DataFrame(SmsList)
        sms_df['ApplyDate'] = ApplyDate
        sms_df['ApplyNO'] = ApplyNO

        # 3 APP特征集
        start = time.time()
        app_features = app.get_free_app_features(app_df)
        free_features.update(app_features)
        end = time.time()
        running_time = end - start
        # print('3：APP特征模块计算耗费时间 : %.5f 秒' % running_time)

        # 3 SMS特征集
        start = time.time()
        sms_features = sms.get_free_sms_features(sms_df)
        free_features.update(sms_features)
        end = time.time()
        running_time = end - start
        # print('4: SMS特征模块计算耗费时间 : %.5f 秒' % running_time)


        # 4 用户评分模块
        start = time.time()
        model_score_features = md.get_user_model_score_main(free_features)
        free_features.update(model_score_features)
        end = time.time()
        running_time = end - start
        # print('5: 用户评分模块耗费时间 : %.5f 秒' % running_time)
        
        return model_score_features

    except Exception as e:
        # utils.get_logger().error(e)
        return {}

if __name__ == '__main__':
    # 新客版本第4版，测试
    ApplyNO = '171422976078363177'
    free_features = get_free_features_v1(ApplyNO)

    print(free_features)


